package com.project.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.StockShopBean;
import com.project.mapper.IStockShopMapper;
import com.project.service.IStockShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class StockShopServiceImp implements IStockShopService {
    @Autowired
    private IStockShopMapper mapper;

    @Override
    public StockShopBean findById(String id) {
        return mapper.selectById(id);
    }

    @Override
    public IPage findByItem(Integer pageNO, String name, String salePrice, String type, Integer startNum, Integer endNum) {
        QueryWrapper qw =new QueryWrapper<>();

        //选的不是不限，就查询null
        if(salePrice!=null&&!salePrice.equals("")&&!salePrice.equals("不限")) {
           qw.isNull("s_salePrice");
        }
        if(type!=null&&!type.equals("")) {
            qw.like("s_type",type);
        }
        if(name!=null&&!name.equals("")){
            qw.like("s_name",name);
        }
        if(startNum!=null){
            qw.ge("s_num",startNum);
        }
        if(endNum!=null){
            qw.le("s_num",endNum);
        }
        return mapper.selectPage(new Page<>(pageNO,5),qw);
    }

    @Override
    public void update(String id, Integer price) {
        StockShopBean stockShopBean = mapper.selectById(id);
        stockShopBean.setSalePrice(price);
        mapper.updateById(stockShopBean);
    }

    @Override
    public void add(StockShopBean stockShopBean) {
        mapper.insert(stockShopBean);
    }
}
